package com.example.picture_sharing_system;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import utils.BaseResponse;
import utils.Constants;
import utils.Users;

public class UserInfoActivity extends AppCompatActivity {


    private EditText my_username;
    private EditText my_age;
    private EditText my_university;
    private EditText my_college;
    private EditText my_major;
    private RadioGroup my_sex;
    private ImageView img_back;
    private Button btn_cancel;
    private Button btn_confirm;
    private int userid;
    private LoadingDailog loadingDailog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        initData();


        getUserinfo();

    }


    public void initData() {
        loadingDailog = new LoadingDailog.Builder(this)
                .setMessage("加载中")
                .setCancelable(true)
                .setCancelOutside(true).create();
        loadingDailog.show();

        userid = getIntent().getIntExtra("userid", 0);

        my_username = findViewById(R.id.my_username);
        my_age = findViewById(R.id.my_age);
        my_college = findViewById(R.id.my_college);
        my_university = findViewById(R.id.my_university);
        my_major = findViewById(R.id.my_major);
        my_sex = findViewById(R.id.my_sex);
        img_back = findViewById(R.id.img_back);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_confirm = findViewById(R.id.btn_confirm);

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadingDailog = new LoadingDailog.Builder(UserInfoActivity.this)
                        .setMessage("加载中")
                        .setCancelable(true)
                        .setCancelOutside(true).create();
                loadingDailog.show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RadioButton rb = (RadioButton) findViewById(my_sex.getCheckedRadioButtonId());
                        String urlParams
                                = "?userid=" + userid
                                + "&sex=" + rb.getText().toString()
                                + "&age=" + my_age.getText().toString()
                                + "&university=" + my_university.getText().toString()
                                + "&college=" + my_college.getText().toString()
                                + "&major=" + my_major.getText().toString()
                                + "&username=" + my_username.getText().toString();

                        Request request = new Request.Builder()
                                .url(Constants.EDIT_USERINFO_URL + urlParams)
                                .get().build();

                        try {
                            OkHttpClient client = new OkHttpClient();
                            client.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                    e.printStackTrace();
                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    if (response.isSuccessful()) {
                                        final String body = response.body().string();
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Gson gson = new Gson();
                                                Type jsonType =
                                                        new TypeToken<BaseResponse<Users>>() {
                                                        }.getType();
                                                BaseResponse<Users> usersBaseResponse =
                                                        gson.fromJson(body, jsonType);

                                                my_username.setText(usersBaseResponse.getData().getUsername());

                                                my_age.setText(usersBaseResponse.getData().getAge());
                                                if (usersBaseResponse.getData().getSex() == null) ;
                                                else if (usersBaseResponse.getData().getSex().equals("男"))
                                                    my_sex.check(R.id.sex_male);
                                                else
                                                    my_sex.check(R.id.sex_female);
                                                my_university.setText(usersBaseResponse.getData().getUniversity());
                                                my_college.setText(usersBaseResponse.getData().getCollege());
                                                my_major.setText(usersBaseResponse.getData().getMajor());
                                                Log.d("TAG", "run: " + usersBaseResponse.getData().getMajor());

                                                loadingDailog.hide();
                                                loadingDailog.dismiss();
                                                Toast.makeText(UserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();


                                            }
                                        });
                                    }
                                }
                            });
                        } catch (NetworkOnMainThreadException ex) {
                            ex.printStackTrace();
                        }
                    }
                }).start();
            }

        });
    }


    public void getUserinfo() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String urlParams
                        = "?userid=" + userid;

                Request request = new Request.Builder()
                        .url(Constants.EDIT_USERINFO_URL + urlParams)
                        .get().build();

                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            if (response.isSuccessful()) {
                                final String body = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson = new Gson();
                                        Type jsonType =
                                                new TypeToken<BaseResponse<Users>>() {
                                                }.getType();
                                        BaseResponse<Users> usersBaseResponse =
                                                gson.fromJson(body, jsonType);

                                        my_username.setText(usersBaseResponse.getData().getUsername());

                                        my_age.setText(usersBaseResponse.getData().getAge());
                                        if (usersBaseResponse.getData().getSex() == null) ;
                                        else if (usersBaseResponse.getData().getSex().equals("男"))
                                            my_sex.check(R.id.sex_male);
                                        else
                                            my_sex.check(R.id.sex_female);
                                        my_university.setText(usersBaseResponse.getData().getUniversity());
                                        my_college.setText(usersBaseResponse.getData().getCollege());
                                        my_major.setText(usersBaseResponse.getData().getMajor());
                                        loadingDailog.hide();
                                        loadingDailog.dismiss();

                                    }
                                });
                            }
                        }
                    });
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onBackPressed() {

        finish();

    }
}